package com.wskj.workbench.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.workbench.bo.web.TbUserJoinOrderDetailAddBo;
import com.wskj.workbench.bo.web.TbUserJoinOrderDetailEditBo;
import com.wskj.workbench.bo.web.TbUserJoinOrderDetailQueryBo;
import com.wskj.workbench.domain.TbUserJoinOrderDetail;
import com.wskj.workbench.mapper.TbUserJoinOrderDetailMapper;
import com.wskj.workbench.service.ITbUserJoinOrderDetailService;
import com.wskj.workbench.vo.web.TbUserJoinOrderDetailVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户入驻申请订单详情Service业务层处理
 *
 * @author wskj
 * @date 2024-09-25
 */
@Service
public class TbUserJoinOrderDetailServiceImpl extends ServicePlusImpl<TbUserJoinOrderDetailMapper, TbUserJoinOrderDetail> implements ITbUserJoinOrderDetailService {

    @Override
    public TbUserJoinOrderDetailVo queryById(Long id){
        return getVoById(id, TbUserJoinOrderDetailVo.class);
    }

    @Override
    public TableDataInfo<TbUserJoinOrderDetailVo> queryPageList(TbUserJoinOrderDetailQueryBo bo) {
        PagePlus<TbUserJoinOrderDetail, TbUserJoinOrderDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), TbUserJoinOrderDetailVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<TbUserJoinOrderDetailVo> queryList(TbUserJoinOrderDetailQueryBo bo) {
        return listVo(buildQueryWrapper(bo), TbUserJoinOrderDetailVo.class);
    }

    private LambdaQueryWrapper<TbUserJoinOrderDetail> buildQueryWrapper(TbUserJoinOrderDetailQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbUserJoinOrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getJoinOrderId() != null, TbUserJoinOrderDetail::getJoinOrderId, bo.getJoinOrderId());
        lqw.like(StrUtil.isNotBlank(bo.getTikName()), TbUserJoinOrderDetail::getTikName, bo.getTikName());
        lqw.eq(StrUtil.isNotBlank(bo.getTikUid()), TbUserJoinOrderDetail::getTikUid, bo.getTikUid());
        lqw.eq(bo.getTikFans() != null, TbUserJoinOrderDetail::getTikFans, bo.getTikFans());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), TbUserJoinOrderDetail::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(TbUserJoinOrderDetailAddBo bo) {
        TbUserJoinOrderDetail add = BeanUtil.toBean(bo, TbUserJoinOrderDetail.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(TbUserJoinOrderDetailEditBo bo) {
        TbUserJoinOrderDetail update = BeanUtil.toBean(bo, TbUserJoinOrderDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TbUserJoinOrderDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importTbUserJoinOrderDetail(List<TbUserJoinOrderDetailVo> tbUserJoinOrderDetailList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(tbUserJoinOrderDetailList), "导入数据不能为空");
        int successNum = 0;
        for (TbUserJoinOrderDetailVo tbUserJoinOrderDetail : tbUserJoinOrderDetailList) {
            TbUserJoinOrderDetail add = BeanUtil.toBean(tbUserJoinOrderDetail, TbUserJoinOrderDetail.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", tbUserJoinOrderDetailList.size(), successNum);
        if (successNum != tbUserJoinOrderDetailList.size()) {
            msg = msg + String.format("失败 %d 条！", tbUserJoinOrderDetailList.size() - successNum);
        }
        return msg;
    }
}
